﻿using UnityEngine;
using System.Collections;
using GDGeek;

/// <summary>
/// 象棋游戏本身的控制，是红方先走还是黑方先走，每方的局面分数是多啊。还不去管是人机对战还是网络对战等
/// </summary>
public class ChessGameController {
    public ChessGameModel chessGameModel { get; private set; }

    private FSM fsm_ = null;

    public ChessGameController ( ) {
        Create( );
        Init( );
    }

    void Create ( ) {
        chessGameModel = new ChessGameModel(RedBlackSide.RED);
        fsm_ = new FSM( );
    }

    private void Init ( ) {
        InitFSM( );
    }

    void InitFSM ( ) {
        fsm_.addState("begin", BeginState( ));
        fsm_.addState("play", PlayState( ));
        fsm_.addState("end", EndState( ));
        fsm_.init("begin");
    }
    
    public void fsmPost (string msg) {
        fsm_.post(msg);
    }

    State BeginState ( ) {
        StateWithEventMap state = new StateWithEventMap( );
        state.onStart += ( ) => {
            Debuger.Log("开始 BeginState");
        };
        state.onOver += ( ) => {
            Debuger.Log("结束 BeginState");
        };

        state.addEvent("begin", "play");
        return state;
    }

    State PlayState ( ) {
        StateWithEventMap state = TaskState.Create(( ) => {
            TaskWait tw = new TaskWait( );
            tw.setAllTime(3f);
            return tw;
        }, fsm_, "end");

        state.onStart += delegate {
            Debuger.Log("开始 PlayState");
        };
        state.onOver += delegate {
            Debuger.Log("开始 PlayState");
        };
        return state;
    }

    State EndState ( ) {
        StateWithEventMap state = new StateWithEventMap( );
        state.addEvent("end", "begin");
        state.onStart += delegate {
            Debuger.Log("开始 EndState");
        };
        state.onOver += delegate {
            Debuger.Log("开始 EndState");
        };
        return state;
    }

}
